package com.situ.ssh.action;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.hibernate.criterion.DetachedCriteria;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.situ.ssh.vo.SearchCondition;

public class BaseAction<T> extends ActionSupport implements ModelDriven<T>, RequestAware {
	// 通过模型驱动方式注入
	// private Student student;
	protected T model;
	protected Integer page;// 第几页
	protected Integer limit;// 每页有多少条数据
	protected DetachedCriteria detachedCriteria;// 离线查找
	protected SearchCondition searchCondition = new SearchCondition();

	// Struts封装的Request
	protected Map<String, Object> requestMap;
	// 批量删除
	protected String ids;

	public BaseAction() {
		// this:当前运行的类(AdminDaoImpl/StudentDaoImpl)
		// this.getClass:当前运行类的字节码:AdminDaoImpl.class/StudentDaoImpl.class
		// this.getClass().getGenericSuperclass():当前运行类的父类即为：BaseDaoImpl<Admin>
		Type type = this.getClass().getGenericSuperclass();
		// 强制转化为参数化类型BaseDaoImpl<Admin.clsss>
		ParameterizedType superClass = (ParameterizedType) type;
		// BaseDaoImpl<Admin,Student>参数可以有多个
		Type[] actualTypeArguments = superClass.getActualTypeArguments();// [Admin.class]
		// 获取数组中第一个元素
		Class<T> entityClass = (Class<T>) actualTypeArguments[0];
		detachedCriteria = DetachedCriteria.forClass(entityClass);
		searchCondition.setDetachedCriteria(detachedCriteria);
		try {
			// student = new Student();
			model = entityClass.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	@Override
	public T getModel() {
		return model;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
		searchCondition.setPage(page);
	}

	public Integer getLimit() {
		return limit;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
		searchCondition.setLimit(limit);
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		this.requestMap = request;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}
}
